import SpeckText from './speckText';
import { Log } from './log';
import {Message, Notification, MessageBox} from 'element-ui';
const cons = new Log();
const WEBSOCKET_MESSAGE_CODE_LIST = [
  {
    code: 'TEST_testSendAll',
    remarks: '测试系统-面向全体 发送消息',
    body: {
      msg: '',
    },
    callbackAfter: function (vue, body) {
      cons.l = body.msg;
      cons.log();
    },
  },
  {
    code: 'TEST_testSendAll_Notification',
    remarks: '测试系统-面向全体 发送通知 Notification',
    body: {
      messageType: '',
      title: '',
      type: '',
      message: '',
    },
    callbackAfter: function () {
      cons.log();
    },
  },
  {
    code: 'TEST_testSendAll_Message',
    remarks: '测试系统-面向全体 发送通知 Message',
    body: {
      messageType: '',
      title: '',
      type: '',
      message: '',
    },
    callbackAfter: function () {
      cons.log();
    },
  },
  {
    code: 'TEST_testSendAll_Alert',
    remarks: '测试系统-面向全体 发送通知 Alert',
    body: {
      messageType: '',
      title: '',
      type: '',
      message: '',
    },
    callbackAfter: function () {
      cons.log();
    },
  },
  {
    code: 'TEST_testSendAll_Speck',
    remarks: '测试系统-面向全体 发送语音播报',
    body: {
      Speck: '',
    },
    callbackAfter: function () {
      cons.log();
    },
  },
  {
    code: 'TEST_testSendOne',
    remarks: '测试系统-单体一个人 发送消息',
    body: {
      msg: '',
      people: '',
    },
  },
  {
    code: 'TEST_testSendOne',
    remarks: '测试系统-群组 发送消息',
    body: {
      msg: '',
      group: '',
    },
  },
];

class WebSocketMessage {
  constructor (Vue, MessageCode) {
    this._vue = Vue;
    this._code = MessageCode;
  }
  init (Vue) {
    this._vue = Vue;
  }
  get vue () {
    return this._vue;
  }
  set code (code) {
    let _code = '';
    WEBSOCKET_MESSAGE_CODE_LIST.forEach(item => {
      if (item.code === code) {
        _code = item;
      }
    });
    if (_code !== '') {
      this._code = _code;
    } else {
      console.error(`未能找到 ${code} 所对应的 code 编码！`);
    }
  }
  send (value) {
    if (typeof this._code !== 'undefined') {
      this._vue.ws.send(JSON.stringify({
        code: this._code.code,
        body: value,
      }));
    } else {
      this._vue.ws.send(value);
    }
  }
  callAfter (message) {
    try {
      let json = JSON.parse(message);
      WEBSOCKET_MESSAGE_CODE_LIST.forEach(item => {
        if (item.code === json.code) {
          cons.lH = item.remarks;
          let data = json.body;
          let {
            messageType,
            title,
            type,
            message,
            Speck,
          } = data;
          if (typeof messageType !== 'undefined') {
            switch (messageType) {
            case 1:
              Notification({
                type,
                title,
                message,
              });
              break;
            case 2:
              Message({
                type,
                message,
              });
              break;
            case 3:
              MessageBox.alert({
                type,
                title,
                message,
              });
              break;
            default:
              break;
            }
            delete data.messageType;
            delete data.title;
            delete data.type;
            delete data.message;
          }
          if (typeof Speck !== 'undefined') {
            SpeckText(Speck);
          }
          item.callbackAfter(this._vue, data);
        }
      });
    } catch (err) {
      console.error('WebSocketMessage message 不是标准格式！' + message);
    }
  }
}
export { WebSocketMessage };
